package com.wwwarehouse.dubbo;

import com.alibaba.dubbo.common.utils.PojoUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;

public class JsonSeriTest {
    public static void main(String[] args) {
        Model<SubModel> modelModel = new Model<>();
        modelModel.setName("test");
        modelModel.setCollection(Lists.newArrayList("abc", "def"));
        SubModel subModel = new SubModel();
        subModel.setName("subModel");
        HashMap<String, SubModel> map = new HashMap<>();
        map.put("key", subModel);
        map.put("key2", subModel);
        modelModel.setMap(map);

        String str = JSON.toJSONString(modelModel);

        Model m = JSON.parseObject(str, Model.class);
        System.out.println(m);
        Type type = new TypeReference<Model<SubModel>>() {}.getType();
        Model m2 = JSON.parseObject(str, type);
        System.out.println(m2);

        pojoUtiles(Lists.newArrayList(modelModel));
    }

    public static void pojoUtiles(Collection<Model<SubModel>> modelModel) {
        Object paramGeneralized = PojoUtils.generalize(modelModel);

        Collection exportParamRestored = (Collection) PojoUtils.realize(paramGeneralized, Collection.class);

        System.out.println(exportParamRestored);
    }
}
